import $service from '../../../service.js';
var that;
const app = getApp();
let page = 1;
Page({
    data:{
        hospitalType: $service.hospitalType,
        doctorList:[],
        departmentList:[],
        doctorName:'',
        departmentId:'',
        isLoginshow:false,
        departmentListIndex: '',
        departmentName: '',
        onMore:false,
        inquiryIndex: '',
        inquiryList: [{
            text: '家庭医生',
            value: '3'
        },{
            text: '复诊',
            value: '4'
        },{
            text: '视频问诊',
            value: '1'
        },{
            text: '图文问诊',
            value: '2'
        }]
    },
    onLoad(e){
        //加载本页面的tabBar样式
        // 如果是首页复诊按钮跳转筛选复诊类型
        this.data.inquiryIndex = e.type ? 1 : ''
        app.editTabBar1();
        this.setData({
            departmentId: e.departmentId,
            inquiryIndex: this.data.inquiryIndex
        })
        this.getDepartmentListPc()
    },
    onShow(){
        page = 1;
        if (!$service.getStore('userInfo')){
            this.setData({
                isLoginshow : true
            })
        } else {
          this.selectComponent("#doctorList").getConsultationFee()
          this.socketInit();
        }
        that = this.data;
        wx.hideHomeButton()
        this.getHomeDoctorList();
    },
    socketInit() {
        app.webSocket.initWebSocket({ url: $service.wsUrl})
        app.webSocket.onReceivedMsg(res => {
          console.log(res)
          if (res.packageType == 12) {
            page = 1
            this.getHomeDoctorList();
          }
        })
      },
    // 获取科室字典
    getDepartmentListPc() {
        app.http.getDepartmentListPc().then(res => {
            if (that.departmentId) {
                that.departmentName = res.data.list.find(item => item.id == that.departmentId).departmentName
            }
            this.setData({
                departmentName: that.departmentName,
                departmentList: res.data.list
            })
        })
    },
    // 获取医生列表
    getHomeDoctorList() {
        let {inquiryList, inquiryIndex} = this.data
        let params = {
            "current": page,
            "departmentId": that.departmentId,
            "parametn": that.doctorName,
            "inquiryType": inquiryIndex ? inquiryList[inquiryIndex].value : '',
            "size": 10
          }
        app.http.getHomeDoctorList(params, true).then(res => {
            let list = $service.deepClone(res.data.list)
            if (that.noMore) return
            if (list.length == 0) {
                this.setData({
                    noMore: true
                })
            }
            if (page != 1) {
                list = that.doctorList.concat(list)
            }
            list.forEach((item, index) => {
                if (item.isOnline === 1) {
                    list.splice(index, 1);
                    list.unshift(item)
                }
            })
            this.setData({
                doctorList: list,
            })
        }).catch(err => {
            $service.showToast(err.msg)
        }).finally(res => {
            wx.stopPullDownRefresh()
        })
    },
    //监听input框
    inputChange(e){
        this.setData({
            doctorName : e.detail.value
        })
        this.getHomeDoctorList()
    },
    //科室选择
    departmentChange(e) {
        page = 1;
        let index = e.detail.value;
        console.log(that.departmentList[index]);
        this.setData({
            noMore: false,
            departmentId: that.departmentList[index].id,
            departmentName: that.departmentList[index].departmentName
        })
        this.getHomeDoctorList()
    },
    // 选择问诊类型
    inquiryChange(e) {
        this.setData({
            inquiryIndex: e.detail.value
        })
        this.getHomeDoctorList()
    },
    onPullDownRefresh() {
        this.setData({
            noMore: false
        })
        page = 1;
        this.getHomeDoctorList();
    },
    onReachBottom() {
        if (!that.onMore) {
            page ++
            this.getHomeDoctorList();
        }
    },
    goLogin() {
        $service.openPage('/pages/login/login')
    },
    goCase() {
        if(this.data.isLoginshow ==true){//判断是否登录
            $service.loginuserid()
            return
          }
        $service.openPage('/pages/user_side/inquiry_service/inquiry_internet_interrogation/inquiry_internet_interrogation')
    }
})